package com.example.proxy2;

import com.example.aspect.MyAspect;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class UserDaoProxy2 implements MethodInterceptor {
    private MyAspect myAspect; //切面对象

    //创建代理，将被代理对象和切面关联在一起
    public UserDaoProxy2(MyAspect myAspect) {
        this.myAspect = myAspect;
    }

    //回调函数
    @Override
    public Object intercept(Object proxy, Method method, Object[] args,
                            MethodProxy methodProxy) throws Throwable {
        System.out.println("---执行MethodInterceptor接口的回调函数intercept---");
        // 前增强
        myAspect.checkPermissions();
        // 目标方法执行
        Object obj = methodProxy.invokeSuper(proxy, args);
        // 后增强
        myAspect.log();
        //返回执行结果
        return obj;
    }
}
